package com.example.fwcz.controller;

import com.example.fwcz.entity.*;

import com.example.fwcz.model.vo.Result;
import com.example.fwcz.service.HouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class HouseController {
    @Autowired
    HouseService houseService;

    /**
     * 增加房屋信息
     * @param houseInfo
     */
    @PostMapping("/houseadd")
    public Result addHouse(@RequestBody houseInfo houseInfo){
        houseService.addHouse(houseInfo);
        return Result.ok("添加房屋信息成功");
    }

    /**
     * 删除房屋信息
     * @param id
     */
    @DeleteMapping("/housedelete")
    public Result deleteHouse(@RequestParam int id){
        houseService.deleteHouse(id);
        return Result.ok("删除房屋信息成功");
    }

    /**
     * 修改房屋信息
     * @param houseInfo
     */
    @PutMapping("/housechange")
    public Result changeHouse(@RequestBody houseInfo houseInfo){
        houseService.changeHouse(houseInfo);
        return Result.ok("修改房屋信息成功");
    }

    /**
     * 搜索所有的房屋信息
     * @param pageNum
     * @param pageSize
     * @returnString
     */
    @GetMapping("/houseall")
    public Result searchAllhouse(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize){
        String orderBy = "price desc";
        PageHelper.startPage(pageNum, pageSize, orderBy);
        PageInfo<houseInfo> pageInfo = new PageInfo<>(houseService.searchAllhouse());
        return Result.ok("请求成功",pageInfo);
    }

    /**
     * 根据 房东【用户名】 搜索房屋信息
     * @param landlord_id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/houselandlord")
    public Result searchLandlordhouse(String landlord_id,
                                      @RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "10") Integer pageSize){
        String orderBy="price desc";
        PageHelper.startPage(pageNum,pageSize,orderBy);
        PageInfo<houseInfo> pageInfo=new PageInfo<>(houseService.searchLandlordhouse(landlord_id));
        return Result.ok("请求成功",pageInfo);
    }

    /**
     * 根据 用户 【用户名】 搜索房屋信息
     * @param user_id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/houseuser")
    public Result searchUserhouse(String user_id,
                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize){
        String orderBy="price desc";
        PageHelper.startPage(pageNum,pageSize,orderBy);
        PageInfo<houseInfo> pageInfo=new PageInfo<>(houseService.searchUserhouse(user_id));
        return Result.ok("请求成功",pageInfo);
    }

    /**
     * 根据 房屋 ID 搜索房屋信息
     * @param id
     * @return
     */
    @GetMapping("/houseone")
    public Result searchOnehouse(String id){
        return Result.ok("请求成功",houseService.searchOnehouse(id));
    }
}
